package my.ts.code;

import java.util.ArrayList;

import my.ts.code.adapter.CategoryAdapter;
import my.ts.code.listener.ConfirmListener;
import my.ts.code.net.AppContext;
import my.ts.code.objects.Item;
import my.ts.code.objects.Root;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.sax.RootElement;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CategoryScreen extends BaseActivity implements ConfirmListener {

	ListView listview;
	TextView tvTitle;
	CategoryAdapter adapter;
	Root root;
	int id;
	String title;
	private LinearLayout mainbgr;
	private Context context;
	String n = "10";
	int p = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		context = this;
		setCListener(this);

		AppContext.instance.session.setListener(this);
		setContentView(R.layout.category_screen);
		init();

		showDialogLoading(this, "Loading Data");

		getData(id, n, p);

	}

	@Override
	public void onResume() {

		super.onResume();
		AppContext.instance.session.setListener(this);

	}

	public void getData(int id, String n, int p) {
		switch (id) {
		case Config.TIN_TUC:
			AppContext.instance.session.getCategoryNews();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;
		case Config.TRUYEN:
			AppContext.instance.session.getCategoryStory();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;

		case Config.GAME:
			AppContext.instance.session.getGameAlbum();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;

		case Config.HINH_ANH:
			AppContext.instance.session
					.getImageCategories(n, String.valueOf(p));
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;

		case Config.VIDEO:
			AppContext.instance.session.getVideoAlbum();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;

		case Config.MUSIC:
			AppContext.instance.session.getMusicCategories();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;

		case Config.AUDIO:
			AppContext.instance.session.getAudioCategories();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;

		case Config.POSTCARD:
			AppContext.instance.session.getPostCardAlbum();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;
		case Config.LOTTERY:
			initCateForLottery();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;
		case Config.CURRENCY:
			initCateForMarket();
			mainbgr.setBackgroundResource(R.drawable.bgr_all);
			break;

		default:
			break;
		}
	}

	private void initCateForMarket() {
		dialog.dismiss();

		root = new Root();
		Item item1 = new Item();
		item1.setCategoryName("Ngoại tệ");
		item1.setCategoryID("0");
		root.getItems().add(item1);

		Item item2 = new Item();
		item2.setCategoryName("Giá vàng");
		item2.setCategoryID("1");
		root.getItems().add(item2);

		adapter = new CategoryAdapter(getBaseContext(), 0, root.getItems());
		listview.setAdapter(adapter);
		adapter.notifyDataSetChanged();

	}

	private void initCateForLottery() {
		dialog.dismiss();

		root = new Root();
		Item item1 = new Item();
		item1.setCategoryName("Kết quả");
		item1.setCategoryID("0");
		root.getItems().add(item1);

		Item item2 = new Item();
		item2.setCategoryName("Soi cầu");
		item2.setCategoryID("1");
		root.getItems().add(item2);

		adapter = new CategoryAdapter(getBaseContext(), 0, root.getItems());
		listview.setAdapter(adapter);
		adapter.notifyDataSetChanged();

	}

	public void init() {

		mainbgr = (LinearLayout) findViewById(R.id.mainbgr);

		tvTitle = (TextView) findViewById(R.id.header);

		Bundle b = getIntent().getExtras();
		id = b.getInt("id");
		title = b.getString("title");

		tvTitle.setText(title);

		listview = (ListView) findViewById(R.id.list);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parents, View arg1,
					int postion, long idex) {
				if (root.getItems().get(postion).getMore() == 1) {
					getData(id, n, p + 1);
					p += 1;
					root.getItems().remove(root.getItems().size() - 1);
					adapter.notifyDataSetChanged();
				} else {
					if (id == Config.CURRENCY) {
						ArrayList<String> titles = new ArrayList<String>();
						ArrayList<String> sids = new ArrayList<String>();
						int s = root.getItems().size();
						for (int i = 0; i < s; i++)
							if (root.getItems().get(i).getMore() == 0) {
								titles.add(root.getItems().get(i)
										.getCategoryName());
								sids.add(root.getItems().get(i).getCategoryID());
							}

						Intent intent = new Intent(CategoryScreen.this,
								ContentScreen.class);
						Bundle b = new Bundle();
						b.putInt("id", id);
						b.putInt("pos", postion);

						b.putStringArrayList("sid", sids);
						b.putStringArrayList("title", titles);

						startActivity(intent.putExtras(b));

					} else {

						Intent intent = new Intent(CategoryScreen.this,
								HeaderScreen.class);
						Bundle b = new Bundle();
						b.putInt("id", id);
						b.putInt("layer", 1);
						b.putString("title", root.getItems().get(postion)
								.getCategoryName());
						b.putString("sid", root.getItems().get(postion)
								.getCategoryID());
						startActivity(intent.putExtras(b));
					}
				}
			}
		});
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {

				Root r = (Root) msg.obj;

				if (r.getMessage() != null) {
					showDialogConfirmSMS(context, CategoryScreen.this,
							r.getMessage(), "Thông báo",
							Config.CHARGE_7_SYNTAX.getSyntax() + "15000 "
									+ Config.TSM,
							Config.CHARGE_7_SYNTAX.getNumber(), false, true);
				} else {

					if (p == 1) {
						root = r;
						adapter = new CategoryAdapter(getBaseContext(), 0,
								root.getItems());
						listview.setAdapter(adapter);
					} else {
						root.setMore(r.isMore());
						// root.getItems().remove(root.getItems().size() -
						// 1);
						int s = r.getItems().size();
						for (int i = 0; i < s; i++)
							root.getItems().add(r.getItems().get(i));
					}

					if (root.isMore()) {
						Item item = new Item();
						item.setMore(1);
						root.getItems().add(item);
					}
					adapter.notifyDataSetChanged();

				}
			}
		}
	};

	@Override
	public void dispatch(String data) {
		// TODO Auto-generated method stub
		// System.out.println("ON DISPATCH");

		dialog.dismiss();

		Root r = parse(data);

		if (r != null) {
			Message msg = handler.obtainMessage(1, r);
			handler.sendMessage(msg);
		}
	}

	@Override
	public void confirmListener(boolean done) {
		// TODO Auto-generated method stub
		if (done) {
			showDialogLoading(context, "Loading data");
			Thread t = new Thread() {
				public void run() {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					getData(id, n, p);
				}
			};

			t.start();
		} else
			finish();
	}

}
